/** 
 * Copyright 2010 Erlacher Felix, Estgfaeller Wolfgang, Ferula Patrick
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package at.socialconference.web.client.ui;

import java.util.Map;
import java.util.TreeMap;

import at.socialconference.web.client.SCwebService.Util;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.TextBox;

/**
 * This popup fetches information from the DBLP site, offers a list of possible
 * choices for a person and allows the user to pick one of them
 * 
 */
public class DBLPPopup extends PopupPanel implements ClickHandler {

	private TextBox txtBox;

	/**
	 * Sets up the DBLPPopup and calls the getDBLPLinks() method of the
	 * web-service
	 * 
	 * @param txtBox
	 *            Where the link should be set finally
	 * @param firstName
	 * @param lastName
	 */
	public DBLPPopup(TextBox txtBox, String firstName, String lastName) {
		super(true);
		this.txtBox = txtBox;
		setGlassEnabled(true);
		setTitle("Alternatives for " + firstName + " " + lastName);
		Label lblLoading = new Label("loading...");
		lblLoading.setStyleName("textbig");
		setWidget(lblLoading);
		this.center();
		Util.getInstance().getDBLPLinks(firstName, lastName, new AsyncCallbackGetDBLPLinks(this));
	}

	// AsyncCallback implementation of the getDBLPLinks method of the
	// web-service
	private class AsyncCallbackGetDBLPLinks implements AsyncCallback<TreeMap<String, String>> {

		private DBLPPopup ref;

		public AsyncCallbackGetDBLPLinks(DBLPPopup ref) {
			super();
			this.ref = ref;
		}

		public void onFailure(Throwable caught) {
			ref.showValues(null);
		}

		public void onSuccess(TreeMap<String, String> result) {
			ref.showValues(result);
		}

	}

	// Displays a list of persons. The user can choose one of them and the
	// mapping link is set in the TextBox
	private void showValues(TreeMap<String, String> result) {
		if (result == null) {
			Window.alert("Error fetching information");
		} else if (result.size() <= 0) {
			Window.alert("Nothing matching in DBLP");
		} else if (result.get("READYLINK")!=null) {
			txtBox.setText(result.get("READYLINK"));
			this.hide();
			this.clear();	
		} else {
			try {
				FlexTable flexTable = new FlexTable();
				Label lbl = new Label("Choose your entry:");
				lbl.setStyleName("textbig");

				flexTable.setWidget(0, 0, lbl);
				// flexTable.getFlexCellFormatter().setColSpan(0, 0, 2);
				int i = 0;
				for (Map.Entry<String, String> entry : result.entrySet()) {
					RadioButton rb = new RadioButton("dblp", entry.getValue());
					rb.setFormValue(entry.getKey());
					rb.addClickHandler(this);
					flexTable.setWidget(i++, 0, rb);
				}
				this.clear();
				this.hide();
				this.setWidget(flexTable);
				this.center();
				return;
			} catch (Exception e) {
				Window.alert(e.getMessage());
			}
		}
		this.clear();
		this.hide();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event
	 * .dom.client.ClickEvent)
	 */
	public void onClick(ClickEvent event) {
		if (event.getSource() instanceof RadioButton) {
			RadioButton rb = (RadioButton) event.getSource();
			txtBox.setText(rb.getFormValue());
			this.hide();
			this.clear();
		}
	}

}
